<?php

namespace App\Http\Controllers\Client;

use App\Services\RedPackService;
use App\Utils\ApiResponse;
use Illuminate\Http\Request;

class RedPackController extends ClientBase
{
    public function exchangeMoney(Request $request)
    {
        $data = $request->decrypted_data;
        if (!isset($data->redpack_num) || (int) $data->redpack_num <= 0) {
            ApiResponse::fail("红包数量不合法");
        }
        $redPackService = new RedPackService();
        try {
            [$res, $msg] = $redPackService->exchangeMoney($this->currentUser->id, $data->redpack_num);
            if ($res) {
                ApiResponse::success();
            } else {
                ApiResponse::fail("红包余额不足");
            }
        } catch (\Exception $e) {
            ApiResponse::fail($e->getMessage());
        }
    }
}
